//
//  BiPurchaseNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/4/27.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiPurchaseNetEngine.h"

#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiPurchaseParser.h"
#import "ASINetworkQueue.h"

@implementation BiPurchaseNetEngine
SINGLE_INSTANCE_IMPLEMENTION(BiPurchaseNetEngine)

/**
 *   请求采购订单审批列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestPurchaseGetOrderCheckList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getOrderCheckList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_ORDERCHECK_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_ORDERCHECK_LIST];
}

/**
 *   请求采购改装上线列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestPurchaseGetOrderRefitList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getOrderrefitList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_ORDERREFIT_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_ORDERREFIT_LIST];
}


/**
 *   请求采购验收入库列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestPurchaseGetOrderYanshouList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getOrderYanshouList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_ORDERYANSHOU_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_ORDERYANSHOU_LIST];
}

/**
 *   请求订单审批，dict为传入参数字典
 */
- (void)requestPurchaseOrderCheck:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/PurchaseOrderCheck",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_ORDER_CHECK]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_ORDER_CHECK];
}

/**
 *   请求改装厂列表，dict为传入参数字典
 */
- (void)requestPurchaseSppliserList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getSppliser",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_SPPLIER_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_SPPLIER_LIST];
}

/**
 *   请求合同列表，dict为传入参数字典
 */
- (void)requestPurchaseContractList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getContractList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_CONTRACT_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_CONTRACT_LIST];
}

/**
 *   //  获取改装上线审核，dict为传入参数字典
 */
- (void)requestPurchaseRefitCheck:(id)delegate paraDict:(NSDictionary *)dict{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/CarRefitCheck",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_REFIT_CHECK]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_REFIT_CHECK];
}


/**
 *   //  获取改装上线保存，dict为传入参数字典
 */
- (void)requestPurchaseRefitSave:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/savePurcaseOrder",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_REFIT_SAVE]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_REFIT_SAVE];
}


//获取改装上线保存信息图片
- (void)requestPurchaseRefitImage:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/uploadContractImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_REFIT_CONTRACT_IMAGE]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_PURCHASE_REFIT_CONTRACT_IMAGE];
}

/**
 *   ///  获取验收入库图片上传，dict为传入参数字典，image为上传的图片
 */
- (void)requestPurchaseYanshouImage:(id)delegate withImge:(UIImage *)image paraDic:(NSDictionary *)paraDic{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/uploadImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_YANSHOU_IMAGE]];
    
    [self createPostImageRequest:url image:image paraDic:paraDic tag:NET_GET_PURCHASE_YANSHOU_IMAGE];
}


/**
 *   ///  获取验收入库审核，dict为传入参数字典
 */
- (void)requestPurchaseCarRukuCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/CarRukuCheck",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_CARRUKU_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_PURCHASE_CARRUKU_CHECK];
}


/**
 *   ///  获取验收入库保存，dict为传入参数字典
 */
- (void)requestPurchaseCarRukuSave:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/savePurcaseOrder",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_CARRUKU_SAVE]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_CARRUKU_SAVE];
}


/**
 *   //  获取库房，dict为传入参数字典
 */
- (void)requestPurchaseStoreRoomList:(id)delegate paraDict:(NSDictionary *)dict{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/getStoreRoomList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_STOREROOM_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_PURCHASE_STOREROOM_LIST];
}


/**
 * //删除改装上线（验收入库）合同图片
 */
- (void)requestPuchaseRefitImageDel:(id)delegate  paraDic:(NSDictionary *)paraDic{

    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarPurchaseCenter/deleteImg",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_PURCHASE_REFIT_IMAGEDEL]];
    
    [self createPostRequest:url paramDic:paraDic tag:NET_GET_PURCHASE_REFIT_IMAGEDEL];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_GET_PURCHASE_ORDERCHECK_LIST:
        {
            [self onGetCarPurchaseOderCheckList:request];
        }
            break;
        case NET_GET_PURCHASE_ORDERREFIT_LIST:
        {
            [self onGetCarPurchaseOderRefitList:request];
        }
            break;
        case NET_GET_PURCHASE_ORDERYANSHOU_LIST:
        {
            [self onGetCarPurchaseOderYanshouList:request];
        }
            break;
        case NET_GET_PURCHASE_ORDER_CHECK:
        {
            [self onGetCarPurchaseCommonData:request];
        }
            break;
        case NET_GET_PURCHASE_SPPLIER_LIST:
        {
            [self onGetCarPurchaseSppliserList:request];
        }
            break;
        case NET_GET_PURCHASE_CONTRACT_LIST:
        {
            [self onGetCarPurchaseContractList:request];
        }
            break;
        case NET_GET_PURCHASE_REFIT_CHECK:
        case NET_GET_PURCHASE_REFIT_SAVE:
        case NET_GET_PURCHASE_CARRUKU_CHECK:
        case  NET_GET_PURCHASE_CARRUKU_SAVE:
        case NET_GET_PURCHASE_REFIT_CONTRACT_IMAGE:
        case NET_GET_PURCHASE_YANSHOU_IMAGE:
        case NET_GET_PURCHASE_REFIT_IMAGEDEL:
        {
            [self onGetCarPurchaseCommonData:request];
        }
            break;
        case NET_GET_PURCHASE_STOREROOM_LIST:
        {
            [self onGetCarPurchaseStoreRoomList:request];
        }
            break;
        default:
            break;
    }
    
}

- (void)onGetCarPurchaseOderCheckList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary  * dict = [BiPurchaseParser parserCarPurchaseOderCheckListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarPurchaseOderRefitList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dic = [BiPurchaseParser parserCarPurchaseOderRefitListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dic userInfo:nil];
    }
}

- (void)onGetCarPurchaseOderYanshouList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiPurchaseParser parserCarPurchaseOderYanshouListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarPurchaseStoreRoomList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiPurchaseParser parserCarPurchaseStoreRoomListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}


- (void)onGetCarPurchaseCommonData:(ASIHTTPRequest *)request{

    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiPurchaseParser parserCarPurchaseCommonEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarPurchaseSppliserList:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiPurchaseParser parserCarPurchaseSppliserListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

- (void)onGetCarPurchaseContractList:(ASIHTTPRequest *)request{
    
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary * dict = [BiPurchaseParser parserCarPurchaseContractListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}


@end
